package com.putong35.common.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.BindingAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentStatePagerItemAdapter;
import com.putong35.common.R;
import com.putong35.common.app.BaseApplication;
import com.putong35.common.baseactfrag.fragment.ListFragment;
import com.putong35.common.customviews.HorizontalImageLayout;
import com.putong35.common.customviews.ImageViewPager;
import com.putong35.common.utils.ui.GlideImageLoader;
import com.youth.banner.Banner;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by Donald Peng on 2018/4/5.
 */

public class DataBindUtils {


    @BindingAdapter({"bannerImages"})
    public static void bindBanner(Banner banner, List<String> url) {
        banner.setImages(url).setImageLoader(new GlideImageLoader()).start();

    }

    @BindingAdapter({"bannerImages"})
    public static void bindBanner(Banner banner, String url) {
        List<String> urls = new ArrayList<>();
        urls.add(url);
        bindBanner(banner, urls);

    }



    @BindingAdapter({"gridImage"})
    public static void bindGridImage(ImageView imageView, String url) {
        Glide.with(BaseApplication.getAppContext()).load(url).into(imageView);

    }


    @BindingAdapter({"image"})
    public static void imageLoader(ImageView imageView, String url) {
        Log.e("pww", "imageLoader: " + url );
//        url = "http://47.112.147.136/static/common/images/flat-avatar.png";
        if (!TextUtils.isEmpty(url)) {
//            url.replace("../", "/");
        }
        Glide.with(BaseApplication.getAppContext()).load(url).into(imageView);

    }

    @BindingAdapter({"imageList"})
    public static void imageLoader(HorizontalImageLayout lytImageList, List<String> urlList) {
        lytImageList.setImageLayoutRes(R.layout.imageview_wrapper, R.id.ivIcon);
        if (urlList != null && urlList.size() > 0) {
            lytImageList.setVisibility(View.VISIBLE);
            lytImageList.clearImageViews();
            for (String url : urlList) {
                if (!TextUtils.isEmpty(url)) {
                    lytImageList.addImage(url);
                }
            }
        } else {
            lytImageList.setVisibility(View.GONE);
        }

    }




    @BindingAdapter({"image"})
    public static void imageLoader(ImageViewPager imageViewPager, List<String> urls) {
        imageViewPager.setupUrls(urls);

    }

    @BindingAdapter({"pageSize", "tabLayout"})
    public static void setViewPager(ViewPager viewPager, int pageSize, SmartTabLayout tabLayout) {
        Context context = viewPager.getContext();
        if (context instanceof AppCompatActivity) {
            AppCompatActivity activity = (AppCompatActivity) context;
            FragmentPagerItems.Creator creator = FragmentPagerItems.with(activity);
            for (int i = 0; i < pageSize; i++) {
                creator.add("test", ListFragment.class);
            }

            FragmentStatePagerItemAdapter adapter = new FragmentStatePagerItemAdapter(
                    activity.getSupportFragmentManager(), creator.create());
            viewPager.setAdapter(adapter);
            tabLayout.setViewPager(viewPager);
        }

    }




    @BindingAdapter({"dateFormat"})
    public static void bindDate(TextView textView, int timeInSeconds) {
        bindDate(textView, 1000L * timeInSeconds);
    }

    @BindingAdapter({"dateFormat"})
    public static void bindDate(TextView textView, long timeInMills) {
        String formatStr = "MM月dd日 HH:mm";
        SimpleDateFormat format = new SimpleDateFormat(formatStr, Locale.CHINA);
        String dateStr = format.format(new Date(timeInMills));
        textView.setText(dateStr);
    }




}
